package com.xinsmartsky.penmaji.NewGridView;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

/**
 * Created by wangxiao on 15-12-30.
 */

public class ReadHZK {

    public static void main(String[] args) throws IOException {
//        String hz = "你好";
//        ReadHZK16 hzk16 = new ReadHZK16();
//        for (int i = 0; i < hz.length(); i++) {
//            hzk16.ReadHZKnumread(hzk, hz.substring(i, i + 1));
//        }
    }

    /**
     * 获取汉字区位码
     *
     * @param chinese
     * @return 区在0、位在1
     */
    public int[] getQw(String chinese) {
        byte[] bs;
        try {
            bs = chinese.getBytes("GB2312");
            int ret[] = new int[bs.length];
            for (int i = 0; i < bs.length; i++) {
                int a = bs[i];
                if (a < 0) {
                    a = 256 + a;
                }
                int bb = (a - 0x80 - 0x20);
                ret[i] = bb;
            }
            return ret;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 读字库、并显示在console中
     *
     * @param hzk 汉字库hzk16文件全路径
     * @param hz  汉字
     * @throws IOException
     */
    //hz:路径
    public byte[] ReadHZKnumread(String hzk, String hz, int digit) throws IOException {
        int[] res = getQw(hz);
        int qh = res[0];
        int wh = res[1];
        byte[] Array = new byte[32];//只针对12 16矩阵
        for (int ar = 0; ar < Array.length; ar++) {
            Array[ar] = 0;
        }
        switch (digit) {
            case 12: {
                long location = (94 * (qh - 1) + (wh - 1)) * 24L;
                byte[] bs = new byte[24];
                RandomAccessFile r = new RandomAccessFile(new File(hzk), "r");// 只读方式打开
                r.seek(location);// 指定下一次开始位置
                r.read(bs);
                r.close();
                int i, j, k, sub, row, col;
                int m = 0;
                for (i = 0; i < 12; i++) {/* 点阵行数索引 */
                    for (j = 0; j < 2; j++) {/* 点阵左子行右子行索引 */
                        for (k = 0; k < 8; k++) {/* 点阵每个点 */
                            // 判断是0还是1
                            if (((bs[i * 2 + j] >> (7 - k)) & 0x1) == 1) {
                                //下标
                                //坐标为：（i行,8j + k列）新坐标为：（i+4行,8j + k列）
                                row = i + 4;
                                col = 8 * j + k;
//                                sub = (i + (8 * j + k) * 12) / 8;
                                sub = col * 2 + 1 - (row / 8);
                                if(row > 7){
                                    m = row - 8;
                                }else{
                                    m = row;
                                }
                                //转换为每列两个字节
//                                m = (7 - ((i + (8 * j + k) * 12) % 8));
                                Array[sub] |= (1 << m);
                            } else {
                            }
                        }
                    }
                }
                break;
            }
            case 16: {
                long location = (94 * (qh - 1) + (wh - 1)) * 32L;
                // 点阵缓冲 16x16 = 32x8
                byte[] bs = new byte[32];
                RandomAccessFile r = new RandomAccessFile(new File(hzk), "r");// 只读方式打开
                r.seek(location);// 指定下一次开始位置
                r.read(bs);
                r.close();
                int i, j, k, jtmep;
                int m = 0;
                for (i = 0; i < 16; i++) {/* 点阵行数索引 */
                    for (j = 0; j < 2; j++) {/* 点阵左子行右子行索引 */
                        for (k = 0; k < 8; k++) {/* 点阵每个点 */
                            int sub = 16 * j + 2 * k + i / 8;
                            //下标0,1;2,3,,,调换位置
                            if (sub % 2 == 1) {
                                jtmep = (sub - 1);
                            } else {
                                jtmep = (sub + 1);
                            }
                            // 判断是0还是1
                            if (((bs[i * 2 + j] >> (7 - k)) & 0x1) == 1) {
                                //坐标为：（i行,8j + k列）
                                //转换为每列两个字节
                                if ((i + 1) % 8 == 0) {
                                    m = 7;
                                } else {
                                    m = (i + 1) % 8 - 1;
                                }
                                Array[jtmep] |= (1 << m);
                            } else {
                            }
                        }
                    }
                }
                break;
            }
        }
        return Array;
    }
}